package com.gf.gf_data.sys.interceptor;

import com.gf.gf_data.service.AccountService;
import com.gf.gf_data.sys.annotation.Access;
import com.gf.gf_data.util.JWTUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 注解权限处理拦截器
 *
 * Created by Eternal_L on 2018/1/10.
 */
public class PermissionInterceptor implements HandlerInterceptor {

    private static Logger log = LoggerFactory.getLogger(PermissionInterceptor.class);

    @Autowired
    private AccountService accountService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        log.info("注解权限拦截-------------------------PermissionInterceptor->preHandle...");

        HandlerMethod handlerMethod = (HandlerMethod) o;
        // 获取方法
        Method method = handlerMethod.getMethod();
        // 获取方法上的注解
        Access access = method.getAnnotation(Access.class);

        // 获取注解权限值（3个属性）
        if (access == null) return false;
        Set<String> authSet = new HashSet<>();
        if (access.value().length > 0)
            authSet.addAll(Arrays.asList(access.value()));
        if (access.authorities().length > 0)
            authSet.addAll(Arrays.asList(access.authorities()));
        if (access.roles().length > 0)
            authSet.addAll(Arrays.asList(access.roles()));
        // 检查当前用户是都具有权限
        if (authSet.size() > 0) {
            String token = request.getHeader(JWTUtil.HEADER_MARK);
            if ( StringUtils.isNotBlank(token)) {

                System.out.println("token = " + token);

                Set<String> roleSet = accountService.findRolesByToken(token);
                Set<String> permissionSet = accountService.findPermissionsByToken(token);
                roleSet.addAll(permissionSet);

                for (String authStr : roleSet) {
                    if (StringUtils.isNotBlank(authStr) && authSet.contains(authStr))
                        log.info("#####注解权限验证通过####################");
                        // 校验通过返回true, 否则拦截请求
                        return true;
                }
            }
            log.info("Token is null|token = " + token);
        }
        log.info("#####注解权限验证失败####################");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView mav) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {

    }

}
